// pages/weddingMan/team/team.js

import { 		getTeamInfo,
  getTeamMember, } from '../../../api/weddingMan/team'
const NAVLIST = [{
  id: 0,
  name: '团队风采'
}]
Page({

  /**
   * 页面的初始数据
   */
  data: {

    // teamId: '',
    activeNavId: 0,
    navList: NAVLIST,
    showCount: 10,
    currentPage: 1,
    totalPage: 1,

    teamId: null,

    team_name: '',
    team_code: "",
    team_desc: "",
    team_icon: "",
    team_coveer: "",
    members: [],
    canShare: false,
    teamLeader: { // 团长信息
      user_wx: '',
      phonenumber: '',
      user_qm_name: ''
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        teamId: options.id,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton()
    this.initTeamInfo()
    
  },

  previewBanner(){
    let url = this.data.team_coveer
    
    if(!url){
      return
    }
    wx.previewMedia({
      sources: [
        {
          url,
          type: 'image'
        }
      ]
    })
  },
  callPhone() {
    const {
      teamLeader
    } = this.data
    if (!teamLeader.phonenumber) {
      wx.showToast({
        title: '没有电话!',
        icon: 'none'
      })
    } else {
      wx.makePhoneCall({
        phoneNumber: teamLeader.phonenumber,
        success: () => {
          
        }
      })
    }
  },

  addTeam() {
    const {
      teamLeader
    } = this.data
    if(!teamLeader.user_wx){
      wx.showToast({
        title: '未填写微信号!',
        icon: 'none'
      })
      return ;
    }
    wx.setClipboardData({
      data: teamLeader.user_wx,
      success: () => {
        wx.showToast({
          title: '微信号复制成功!',
          icon: 'none'
        })
      },
      fail: () => {
        wx.showToast({
          title: '微信号复制失败!',
          icon: 'none'
        })
      }
    });
  },

  toTeamSchedule() {
    const id = this.data.teamId
    wx.navigateTo({
      url: `/pages/weddingMan/team/scedules/scedules?id=${id}`
    })
  },
  initTeamInfo() {

    const {
      teamId,
      activeNavId
    } = this.data
    if (!teamId) {
      return;
    }
    this.setData({
      currentPage: 1,
      totalPage: 1,
      list: []
    })
    this.getTeamInfo()
    if (activeNavId === 0) {
      this.getTeamMember()
    }
  },
  getTeamInfo() {
    const {
      teamId
    } = this.data
    getTeamInfo({
      id: teamId,
      // showCount,
      // currentPage
    }).then(res => {
      
      const {
        team_name,
        team_code,
        team_desc,
        team_icon,
        is_boss,
        team_coveer
      } = res.data
      const params = {}
      if (team_name) {
        params.team_name = team_name
        wx.setNavigationBarTitle({
          title: `${params.team_name}`
        })
      }
      if (team_desc) {
        params.team_desc = team_desc
      }
      if (team_code) {
        params.team_code = team_code
      }
      if (team_icon) {
        params.team_icon = team_icon
      }

      if (team_coveer) {
        params.team_coveer = team_coveer;
      }
      this.setData({
        ...params
      })

    }).catch(err => {
      
    })
  },
  getTeamMember() {
    const {
      teamId,
      currentPage,
      showCount,
    } = this.data

    if (!teamId) {
      return;
    }
    getTeamMember({
        currentPage,
        showCount,
        team_id: String(teamId)
      }).then(res => {
        
        let members = res.data.list
        this.setData({
          members
        })
        let tmpLen = members.length;
        for (let i = 0; i < tmpLen; i += 1) {
          
          if (members[i] && members[i].isBoss === '1') {
            let teamLeader = {}
            teamLeader.phonenumber = members[i].phonenumber;
            teamLeader.user_wx = members[i].user_wx;
            teamLeader.user_qm_name = members[i].user_qm_name;
            this.setData({
              teamLeader,
            })
            break;
          }
        }
        
        

      })
      .catch(err => {
        
      })
  },
  toHome(e) {
    const { id, isMutualFollow } = e.currentTarget.dataset
    const tmpGzStatus = isMutualFollow === '1' ? '2' : '1';
    wx.navigateTo({
      url: `/pages/weddingMan/personal/personal?id=${id}&gzStatus=${tmpGzStatus}`
    })
  },
  toMemberPage() {
    const {teamId} = this.data
    wx.navigateTo({
      url: `/pages/weddingMan/team/members/members?id=${teamId}`
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: `${this.data.team_name}的主页`,
      path:
        'pages/weddingMan/team/team?id=' +
        this.data.teamId
    };
  },
})